package com.symone.webcms.controller.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.Menu;
import com.symone.webcms.service.MenuService;
import com.symone.webcms.service.MessageService;
import com.symone.webcms.service.NotificationService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class IndexController extends BaseController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private NotificationService notificationService;

    @RequestMapping(value = "/cms/framework", method = RequestMethod.GET)
    public ModelAndView framework() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/framework");
        return mv;
    }

    @RequestMapping(value = "/cms/dashboard", method = RequestMethod.GET)
    public ModelAndView dashboard() {
        List<Menu> menus = menuService.selectList(new EntityWrapper<Menu>().eq("deleted",0).and().eq("short_cut",1));
        ModelAndView mv = new ModelAndView();
        mv.addObject("menus",menus);
        mv.setViewName("/dashboard");
        return mv;
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            mv.setViewName("redirect:/cms/dashboard");
        } else {
            mv.setViewName("login");
            mv.addObject("formcheck", getMvcFormCheck());
        }
        return mv;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
        try {
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
        } catch (Exception ex){
        }
        return "redirect:/";
    }
}
